<template>
	<view>
		<z-nav-bar title="详情"></z-nav-bar>
		<view class="content">
			<image v-if="courseInfo.coverImage" :src="setting.oss_pub_domain + courseInfo.material" mode="aspectFill"></image>
			<view>
				<view class="title">{{ courseInfo.name }}</view>
				<view class="dsc">{{ courseInfo.description }}</view>
				<view class="text">
					<text>{{ courseInfo.tuition }}分</text>
					<text class="num">已有{{ courseInfo.buyCount }}人学习</text>
				</view>
				<view class="entry">
					<text>{{ courseInfo.descriptionTagName }}</text>
				</view>
			</view>
		</view>
		<view class="detail">
			<view class="row" :class="{ active: scheme.vis }" @click="change(k)" v-for="(scheme, k) in courseInfo.schemeList" :key="k">
				<view class="title">
					<view class="bg">
						<text>{{ scheme.name }}</text>
						<view v-if="courseInfo.isBuy" class="view-course" @click.stop="viewScheme(scheme)">查看方案</view>
					</view>
				</view>
			</view>
		</view>
		<view class="use">
			<view>使用说明</view>
			<view class="text">
				<view>1. 体系课默认使用期限为长期</view>
				<view>2. 如违反国家规定的内容将下架处理</view>
				<view>3. 最终解释权为降杆APP所有</view>
			</view>
		</view>
		<view v-if="!courseInfo.isBuy" class="btn" @click="immediateExchange">
			<text>立即兑换</text>
		</view>
	</view>
</template>

<script>
import { getOpenDetail } from '@/api/course';
import { exchangeOpenCourse } from "@/api/student"
export default {
	data() {
		return {
			courseInfo: {}
		};
	},
	onLoad(opt) {
		if (opt.id)
			getOpenDetail(opt.id).then((res) => {
				console.log(res);
				this.courseInfo = res
			});
	},
	methods: {
		change(k) {
			for (let i in this.courseInfo.courseList) {
				if (i == k) {
					if (this.courseInfo.courseList[i].vis) {
						this.courseInfo.courseList[i].vis = false;
					} else {
						this.courseInfo.courseList[i].vis = true;
					}
				}
			}
		},
		viewScheme(item) {
			item.readOnly = 1
			uni.setStorageSync('edit-scheme', item)
			uni.navigateTo({
				url: '/pages/scheme/details'
			})
		},
		immediateExchange() {
			uni.showModal({
				title: '兑换提示',
				content: '确定要兑换该体系课吗？',
				success: res => {
					if (res.cancel) return
					exchangeOpenCourse(this.courseInfo.id).then(res => {
						this.tools.alert('兑换成功')
						this.courseInfo.isBuy = 1
					})
				}
				
			})
		}
	}
};
</script>

<style>
@import './css/course.css';
</style>
